home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWFiles / SLFileSy.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  20.4 KB  |  861 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFileSy.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef SLFILESY_H
  13. #include "SLFileSy.h"
  14. #endif
  15.  
  16. #ifndef FWFILESP_H
  17. #include "FWFileSp.h"
  18. #endif
  19.  
  20. #ifndef FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #ifndef FWEXCDEF_H
  25. #include "FWExcDef.h"
  26. #endif
  27.  
  28. #ifndef FWSTRS_H
  29. #include "FWStrs.h"
  30. #endif
  31.  
  32. #ifndef FWBNDSTR_H
  33. #include "FWBndStr.h"
  34. #endif
  35.  
  36. #ifndef SLFILPAR_H
  37. #include "SLFilPar.h"
  38. #endif
  39.  
  40. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  41. #include <Script.h>
  42. #endif
  43.  
  44. #if defined(FW_BUILD_MAC) && !defined(__FILES__)
  45. #include <Files.h>
  46. #endif
  47.  
  48. #if defined(FW_BUILD_MAC) && !defined(__ERRORS__)
  49. #include <Errors.h>
  50. #endif
  51.  
  52. #if defined(FW_BUILD_WIN) && !defined(FWMEMMGR_H)
  53. #include "FWMemMgr.h"
  54. #endif
  55.  
  56. #if defined(FW_BUILD_WIN) && !defined(__DIRECT_H)
  57. #include <direct.h>
  58. #endif
  59.  
  60. #if defined(FW_BUILD_WIN) && !defined(__DOS_H)
  61. #include <dos.h>
  62. #endif
  63.  
  64. #if defined(FW_BUILD_WIN) && !defined(__IO_H)
  65. #include <io.h>
  66. #endif
  67.  
  68. #if defined(FW_BUILD_WIN) && !defined(__CTYPE_H)
  69. #include <ctype.h>
  70. #endif
  71.  
  72. #ifdef FW_BUILD_WIN16
  73. extern "C" void FAR PASCAL DOS3Call();                // We don't do "int 21h" under Windows
  74. #endif
  75.  
  76.  
  77.  
  78. //========================================================================================
  79. // Constants
  80. //========================================================================================
  81. #ifdef FW_BUILD_MAC
  82. #define FW_kMacVolumeIsSharedBitMask    0x00000200
  83. #endif
  84.  
  85.  
  86.  
  87.  
  88. //========================================================================================
  89. //    Runtime type information
  90. //========================================================================================
  91.  
  92. #ifdef FW_BUILD_MAC
  93. #pragma segment FileSystem
  94. #endif
  95.  
  96. #ifdef FW_BUILD_WIN16
  97. static FW_PlatformError privWinPrimitiveSetCurrentDir(const FW_Char* dirName, short driveNumber);
  98. static FW_PlatformError privWinPrimitiveDeleteDir(const FW_Char* dirName);
  99. static FW_PlatformError privWinPrimitiveCreateDir(const FW_Char* dirName);
  100. #endif
  101.  
  102.  
  103.  
  104.  
  105. //========================================================================================
  106. // FW_PrivFileSystem_
  107. //========================================================================================
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //    FW_PrivFileSystem_CreateFile
  111. //
  112. //  Create the file specified by fileSpec.
  113. //  On the Macintosh, the file is created as a TeachText text file by default.  The user
  114. //    can always change the file type using the FW_CAccessFileInfo helper class.  
  115. //----------------------------------------------------------------------------------------
  116.  
  117. void SL_API 
  118. FW_PrivFileSystem_CreateFile(Environment* ev, 
  119.                              FW_OFileSpecification* fileSpec,
  120.                              FW_Boolean overWriteExisting)
  121. {
  122.     FW_SOM_TRY
  123.     {
  124.         FW_PlatformError theError = FW_xNoError;
  125.  
  126. #ifdef FW_BUILD_WIN
  127.         FW_CString fileName;
  128.     
  129.         fileSpec->GetFullPath(ev, fileName);
  130.     
  131.         char szFileName[_MAX_PATH];
  132.         fileName.ExportCString(szFileName);
  133. #endif
  134.     
  135. #ifdef FW_BUILD_WIN16
  136.         FW_PlatformFileHandle fileHandle;
  137.         OFSTRUCT Buffer;
  138.     
  139.         if (!overWriteExisting)
  140.         {
  141.             fileHandle = ::OpenFile(szFileName, &Buffer, OF_EXIST);
  142.     
  143.             if (fileHandle != FW_kInvalidAccessHandle)
  144.                 FW_Failure(FW_xFileExists);
  145.         }
  146.         
  147.         fileHandle = ::OpenFile((LPCSTR)fileName, &Buffer, OF_CREATE);
  148.         if (fileHandle == FW_kInvalidAccessHandle)
  149.             theError = Buffer.nErrCode;
  150.         else
  151.             ::_lclose(fileHandle);
  152. #endif
  153.     
  154. #ifdef FW_BUILD_WIN32
  155.         FW_PlatformFileHandle fileHandle;
  156.         long createMode = (overWriteExisting ? TRUNCATE_EXISTING | CREATE_NEW : CREATE_NEW);
  157.         
  158.         fileHandle = ::CreateFile(szFileName, 
  159.                                   GENERIC_READ | GENERIC_WRITE, 
  160.                                   0, 
  161.                                   NULL,
  162.                                   createMode, 
  163.                                   FILE_ATTRIBUTE_NORMAL, 
  164.                                   (HANDLE)NULL);
  165.                                    
  166.         if (fileHandle == FW_kInvalidAccessHandle)
  167.             theError = ::GetLastError();
  168. #endif
  169.     
  170. #ifdef FW_BUILD_MAC
  171.         short fileHandle;
  172.         FSSpec theMacSpec;
  173.         OSType aFileType;
  174.         OSType aCreatorType;
  175.     
  176.         fileSpec->MacGetFSSpec(ev, &theMacSpec);
  177.         fileSpec->MacGetTypeAndCreator(ev, &aFileType, &aCreatorType);
  178.     
  179.         theError = ::FSpCreate(&theMacSpec, aCreatorType, aFileType, smSystemScript);
  180.     
  181.         if (overWriteExisting && (theError == FW_xFileExists))
  182.         {
  183.             theError = ::FSpOpenDF(&theMacSpec, FW_kReadWrite, &fileHandle);
  184.             if (theError == FW_xNoError)
  185.             {
  186.                 // Truncate the file.  If an error occurs record it for throwing and close the file.
  187.                 theError = ::SetEOF(fileHandle, 0);
  188.                 if (theError == FW_xNoError)
  189.                     theError = ::FSClose(fileHandle);
  190.                 else
  191.                     ::FSClose(fileHandle);
  192.             }
  193.         }
  194. #endif
  195.     
  196.         FW_FailOnError(theError);
  197.     }
  198.     FW_SOM_CATCH
  199. }
  200.  
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    FW_PrivFileSystem_DeleteFile
  204. //
  205. //  Delete the file specified by fileSpec.  If the file does not exist, throw a 
  206. //    FW_XFileNotFound error containing a copy of the fileSpec.
  207. //----------------------------------------------------------------------------------------
  208.  
  209. void SL_API 
  210. FW_PrivFileSystem_DeleteFile(Environment* ev, FW_OFileSpecification* fileSpec)
  211. {
  212.     FW_SOM_TRY
  213.     {
  214.         FW_PlatformError theError = FW_xNoError;
  215.  
  216. #ifdef FW_BUILD_WIN
  217.         FW_CString fileName;
  218.     
  219.         fileSpec->GetFullPath(ev, fileName);
  220.     
  221.         char szFileName[_MAX_PATH];
  222.         fileName.ExportCString(szFileName);
  223. #endif
  224.  
  225. #ifdef FW_BUILD_WIN16
  226.         OFSTRUCT Buffer;
  227.         if (::OpenFile(szFileName, &Buffer, OF_DELETE) == FW_kInvalidAccessHandle)
  228.             theError = Buffer.nErrCode;
  229. #endif
  230.  
  231. #ifdef FW_BUILD_WIN32
  232.         if(!::DeleteFile(szFileName))
  233.             theError = ::GetLastError();
  234. #endif
  235.  
  236. #ifdef FW_BUILD_MAC
  237.         FSSpec theMacSpec;
  238.     
  239.         fileSpec->MacGetFSSpec(ev, &theMacSpec);
  240.         theError = ::FSpDelete(&theMacSpec);
  241. #endif
  242.  
  243.         FW_FailOnError(theError);
  244.     }
  245.     FW_SOM_CATCH
  246. }
  247.  
  248.  
  249. #ifdef FW_BUILD_WIN16
  250. //----------------------------------------------------------------------------------------
  251. // privWinPrimitiveCreateDir
  252. //----------------------------------------------------------------------------------------
  253. FW_PlatformError
  254. privWinPrimitiveCreateDir(const FW_Char* dirName)
  255. {
  256.     FW_PlatformError theError = FW_xNoError;
  257.     
  258.     __asm {
  259.         push     ds
  260.         lds        dx, [dirName]
  261.         mov     ah, 39h
  262.     }
  263.     
  264.     DOS3Call();
  265.         
  266.     __asm {
  267.         pop        ds
  268.         jc        _error
  269.         xor     ax, ax
  270.     }
  271.             
  272.     _error:
  273.     
  274.     __asm {
  275.         mov        theError, ax
  276.     }
  277.     
  278.     return (theError);
  279. }
  280. #endif
  281.  
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_PrivFileSystem_CreateDirectory
  285. //----------------------------------------------------------------------------------------
  286.  
  287. void SL_API 
  288. FW_PrivFileSystem_CreateDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  289. {
  290.     FW_SOM_TRY
  291.     {
  292.         FW_PlatformError theError = FW_xNoError;
  293.     
  294. #ifdef FW_BUILD_WIN
  295.         FW_CString directoryName;
  296.     
  297.         // Get full path name and remove trailing delimiter.
  298.         //   This should always work since the directory can never be just the root path.
  299.         directory->GetFullPath(ev, directoryName);
  300.         if (directoryName.GetByteLength() > 0)
  301.             directoryName.Truncate(directoryName.GetByteLength() - 1);
  302.     
  303.         char szDirectoryName[_MAX_PATH];
  304.         directoryName.ExportCString(szDirectoryName);
  305. #endif
  306.     
  307. #ifdef FW_BUILD_WIN16
  308.         theError = privWinPrimitiveCreateDir(szDirectoryName);
  309. #endif
  310.     
  311. #ifdef FW_BUILD_WIN32
  312.         if(!::CreateDirectory(szDirectoryName, NULL))
  313.             theError = ::GetLastError();
  314. #endif
  315.     
  316. #ifdef FW_BUILD_MAC
  317.         long directoryID;
  318.         FSSpec macSpec;
  319.         
  320.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &macSpec);
  321.         
  322.         theError = ::FSpDirCreate(&macSpec, smSystemScript, &directoryID);
  323. #endif
  324.         
  325.         FW_FailOnError(theError);
  326.     }
  327.     FW_SOM_CATCH
  328. }
  329.  
  330.  
  331. #ifdef FW_BUILD_WIN16
  332. //----------------------------------------------------------------------------------------
  333. // privWinPrimitiveDeleteDir
  334. //----------------------------------------------------------------------------------------
  335. FW_PlatformError
  336. privWinPrimitiveDeleteDir(const FW_Char* dirName)
  337. {
  338.     FW_PlatformError theError = FW_xNoError;
  339.     
  340.     __asm {
  341.         push     ds
  342.         lds        dx, [dirName]
  343.         mov     ah, 3Ah
  344.     }
  345.     
  346.     DOS3Call();
  347.         
  348.     __asm {
  349.         pop        ds
  350.         jc        _error
  351.         xor     ax, ax
  352.     }
  353.             
  354.     _error:
  355.     
  356.     __asm {
  357.         mov        theError, ax
  358.     }
  359.     
  360.     return(theError);
  361. }
  362. #endif
  363.  
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // FW_PrivFileSystem_DeleteDirectory
  367. //----------------------------------------------------------------------------------------
  368.  
  369. void SL_API 
  370. FW_PrivFileSystem_DeleteDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  371. {
  372.     FW_SOM_TRY
  373.     {
  374.         FW_PDirectorySpecification defaultDirectory;
  375.         
  376.         // if the directory we want to delete is the current directory, then we shouldn't
  377.         //   delete it.
  378.         if (defaultDirectory == directory)
  379.             FW_Failure(FW_xCantDeleteWorkingDirectory);
  380.             
  381.     
  382.         FW_PlatformError theError = FW_xNoError;
  383. #ifdef FW_BUILD_WIN
  384.         FW_CString directoryName;
  385.     
  386.         // Get full path name and remove trailing delimiter.
  387.         //   This should always work since the directory can never be just the root path.
  388.         directory->GetFullPath(ev, directoryName);
  389.         if (directoryName.GetByteLength() > 0)
  390.             directoryName.Truncate(directoryName.GetByteLength()-1);
  391.     
  392.         char szDirectoryName[_MAX_PATH];
  393.         directoryName.ExportCString(szDirectoryName);
  394. #endif
  395.     
  396. #ifdef FW_BUILD_WIN16
  397.         theError = privWinPrimitiveDeleteDir(szDirectoryName);            
  398. #endif
  399.     
  400. #ifdef FW_BUILD_WIN32
  401.         if(!::RemoveDirectory(szDirectoryName))
  402.             theError = ::GetLastError();
  403. #endif
  404.     
  405. #ifdef FW_BUILD_MAC
  406.         FSSpec macSpec;
  407.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &macSpec);
  408.         
  409.         theError = ::FSpDelete(&macSpec);
  410. #endif
  411.     
  412.         FW_FailOnError(theError);
  413.     }
  414.     FW_SOM_CATCH
  415. }
  416.  
  417.  
  418. //----------------------------------------------------------------------------------------
  419. // FW_PrivFileSystem_GetCurrentDirectory
  420. //
  421. //  Returns the current working directory in directory.  
  422. //----------------------------------------------------------------------------------------
  423.  
  424. void SL_API 
  425. FW_PrivFileSystem_GetCurrentDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  426. {
  427.     FW_SOM_TRY
  428.     {
  429.         FW_PlatformError theError = FW_xNoError;
  430.     
  431. #ifdef FW_BUILD_WIN
  432.         FW_CString directoryName;
  433.     
  434.         theError = FW_PrivFileSystemParser_PrivGetWorkingDirectory(directoryName);
  435.         FW_FailOnError(theError);
  436.         directory->AssignFileName(ev, directoryName);
  437. #endif
  438.     
  439.     
  440. #ifdef FW_BUILD_MAC
  441.         FSSpec directoryName;
  442.         
  443.         // Workaround for MPW Shell 3.3 bug when passing 0, 0, NULL to FSMakeFSSpec.
  444.         Str32 nullName = "\p";
  445.         theError = ::FSMakeFSSpec(0, 0, nullName, &directoryName);
  446.         FW_FailOnError(theError);
  447.         directory->AssignFileSpec(ev, &directoryName);
  448. #endif
  449.     }
  450.     FW_SOM_CATCH
  451. }
  452.  
  453.  
  454. #ifdef FW_BUILD_WIN16
  455. //----------------------------------------------------------------------------------------
  456. // privWinPrimitiveSetCurrentDir
  457. //
  458. //  driveNumber should be a number from 0 to 25 where drive A=0, B=1, C=2, etc.
  459. //----------------------------------------------------------------------------------------
  460. FW_PlatformError
  461. privWinPrimitiveSetCurrentDir(const FW_Char* dirName, short driveNumber)
  462. {
  463.     FW_PlatformError theError = FW_xNoError;
  464.  
  465.     __asm {
  466.         mov        dx, driveNumber
  467.         mov     ah, 0Eh
  468.     }
  469.     
  470.     DOS3Call();
  471.         
  472.     // Now, set the directory.
  473.     __asm {
  474.         push     ds
  475.         lds        dx, [dirName]
  476.         mov     ah, 3Bh
  477.     }
  478.     
  479.     DOS3Call();
  480.         
  481.     __asm {
  482.         pop        ds
  483.         jc        _error
  484.         xor     ax, ax
  485.     }
  486.             
  487.     _error:
  488.     
  489.     __asm {
  490.         mov        theError, ax
  491.     }
  492.     
  493.     return (theError);
  494. }
  495. #endif
  496.  
  497.  
  498. //----------------------------------------------------------------------------------------
  499. // FW_PrivFileSystem_SetCurrentDirectory
  500. //
  501. // Sets the default working directory for the application.  This also sets the directory
  502. //   that appears in the standard file package when an SFP dialog is displayed.
  503. //----------------------------------------------------------------------------------------
  504.  
  505. void SL_API 
  506. FW_PrivFileSystem_SetCurrentDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  507. {
  508.     FW_SOM_TRY
  509.     {
  510.         FW_PlatformError theError = FW_xNoError;
  511.         
  512.         if (!FW_PrivFileSystem_IsValidDirectory(ev, directory))
  513.             FW_Failure(FW_xInvalidDirectory);
  514.             
  515. #ifdef FW_BUILD_WIN
  516.         FW_CString directoryName;
  517.         
  518.         // Get the pathname without the trailing delimiter character.
  519.         directory->GetFullPath(ev, directoryName);
  520.         if (directoryName.GetByteLength() > 0)
  521.             directoryName.Truncate(directoryName.GetByteLength() - 1);
  522.     
  523.         char szDirectoryName[_MAX_PATH];
  524.         directoryName.ExportCString(szDirectoryName);
  525. #endif
  526.     
  527. #ifdef FW_BUILD_WIN16
  528.         // Set drive first
  529.         short driveNumber = directory.WinGetDrive() - (FW_Char)('a');
  530.         theError = privWinPrimitiveSetCurrentDir(szDirectoryName, driveNumber);
  531. #endif
  532.     
  533. #ifdef FW_BUILD_WIN32
  534.         if(!::SetCurrentDirectory(szDirectoryName))
  535.             theError = ::GetLastError();
  536. #endif
  537.     
  538. #ifdef FW_BUILD_MAC
  539.         FSSpec macSpec;
  540.     
  541.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &macSpec);
  542.         theError =     ::HSetVol(macSpec.name, macSpec.vRefNum, macSpec.parID);
  543. #endif
  544.     
  545.         FW_FailOnError(theError);
  546.     }
  547.     FW_SOM_CATCH
  548. }
  549.  
  550.  
  551. //----------------------------------------------------------------------------------------
  552. // FW_PrivFileSystem_IsValidDirectory
  553. //
  554. // Given a fully qualified or partial pathname specified in directoryName, IsValidDirectory
  555. //   will return TRUE if the directory exists.  If the directory or drive doesn't exist, 
  556. //   IsValidDirectory will return FALSE.
  557. //----------------------------------------------------------------------------------------
  558.  
  559. FW_Boolean SL_API 
  560. FW_PrivFileSystem_IsValidDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  561. {
  562.     FW_Boolean result = FALSE;
  563.  
  564.     FW_SOM_TRY
  565.     {
  566. #ifdef FW_BUILD_WIN
  567.         FW_TRY
  568.         {
  569.             FW_CString pathName;
  570.             
  571.             directory->GetFullPath(ev, pathName);
  572.             if (pathName.GetByteLength() > 0)
  573.                 pathName.Truncate(pathName.GetByteLength() - 1);
  574.                 
  575.             result = FW_PrivFileSystem_WinPathExists(ev, pathName);
  576.         }
  577.         FW_CATCH_BEGIN
  578.         FW_CATCH_NO_INSTANCE(FW_XException)
  579.         {
  580.             result = FALSE;
  581.         }
  582.         FW_CATCH_EVERYTHING()
  583.         {
  584.             FW_THROW_SAME();
  585.         }
  586.         FW_CATCH_END
  587. #endif
  588.     
  589.     
  590. #ifdef FW_BUILD_MAC
  591.         FSSpec theSpec;
  592.         OSErr theError = FW_xNoError;
  593.         
  594.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &theSpec);
  595.         
  596.         // Try and make an FSSpec out of the directory.  If any error is returned, the
  597.         //   directory cannot exist.
  598.         theError = ::FSMakeFSSpec(theSpec.vRefNum,
  599.                                   theSpec.parID, 
  600.                                   (StringPtr)&(theSpec.name), 
  601.                                   &theSpec);
  602.         if (theError == FW_xNoError)
  603.             result = TRUE;
  604. #endif
  605.     }
  606.     FW_SOM_CATCH
  607.  
  608.     return (result);
  609. }
  610.  
  611.  
  612. //----------------------------------------------------------------------------------------
  613. // FW_PrivFileSystem_IsValidDrive
  614. //
  615. // Given a drive name specified in driveName, IsValidDrive will return TRUE if the drive
  616. //   letter is used.  For removeable media drives, there may not be anything in the drive,
  617. //   but the drive could still be valid.
  618. //----------------------------------------------------------------------------------------
  619.  
  620. FW_Boolean SL_API     
  621. FW_PrivFileSystem_IsValidDrive(Environment* ev, FW_HString driveNameRep)
  622. {
  623.     FW_Boolean result = FALSE;
  624.  
  625.     FW_SOM_TRY
  626.     {
  627.         FW_CString driveName(driveNameRep);
  628. #ifdef FW_BUILD_WIN
  629.         FW_TRY
  630.         {
  631.             if (driveName.GetByteLength() != 0)
  632.             {
  633.                 int driveType = 0;
  634.                 FW_CString32 strTemp(driveName);
  635.                 
  636.                 if (FW_PrivFileSystemParser_WinGetDrivePath(driveName, strTemp))
  637.                 {
  638.                     strTemp.ToLower();
  639.                     if ((strTemp.GetByteLength() > 1) && (strTemp[(FW_CharacterPosition)1] == FW_kDriveDelimiter))
  640.                     {
  641.     
  642. #ifdef FW_BUILD_WIN16
  643.                         FW_Char dwDriveID = strTemp[0];
  644.                         driveType = ::GetDriveType((int)dwDriveID - (int)('a'));
  645. #endif
  646.     
  647.     
  648. #ifdef FW_BUILD_WIN32
  649.                         strTemp += FW_kPathDelimiter;
  650.                         char szTemp[_MAX_PATH];
  651.                         strTemp.ExportCString(szTemp);
  652.                         driveType = ::GetDriveType(szTemp);
  653. #endif
  654.     
  655.                         // A result of 0 or 1 indicates the drive didn't exist.  Otherwise
  656.                         //   the return result is the type of the drive.  
  657.                         if (driveType > 1)
  658.                             result = TRUE;
  659.                     }
  660.                 }
  661.             }
  662.         }
  663.         FW_CATCH_BEGIN
  664.         FW_CATCH_NO_INSTANCE(FW_XException)
  665.         {
  666.             result = FALSE;
  667.         }
  668.         FW_CATCH_EVERYTHING()
  669.         {
  670.             FW_THROW_SAME();
  671.         }
  672.         FW_CATCH_END
  673.         
  674. #endif
  675.  
  676. #ifdef FW_BUILD_MAC
  677.         short defaultVolumeRefNum = 0;
  678.     
  679.         FW_CString32 newDriveName = driveName;
  680.         Str32 macDriveString;
  681.         
  682.         FW_CharacterPosition pathSeparator = 0;
  683.         OSErr theError = FW_xNoError;
  684.     
  685.         // The last character in the string should be a Mac path separator.
  686.         if (newDriveName.FindCharacter(FW_kPathDelimiter, pathSeparator))
  687.             newDriveName.Truncate(pathSeparator + 1);
  688.         else
  689.             newDriveName += FW_kPathDelimiter;
  690.             
  691.         newDriveName.ExportPascal((FW_PascalChar*)&macDriveString);
  692.     
  693.         // Save the default volume and then attempt to set the default volume to
  694.         //   driveName.  Any errors indicates that the drive doesn't exist.
  695.         theError = ::GetVol(NULL, &defaultVolumeRefNum);
  696.         if (theError == FW_xNoError)
  697.         {
  698.             // Use a non-negative, non-zero number for an invalid drive.
  699.             theError = ::SetVol(macDriveString, 1);
  700.     
  701.             // Restore the default volume.
  702.             ::SetVol(NULL, defaultVolumeRefNum);
  703.     
  704.             if (theError == FW_xNoError)
  705.                 result = TRUE;
  706.         }
  707. #endif
  708.     }
  709.     FW_SOM_CATCH
  710.  
  711.     return (result);
  712. }
  713.  
  714.  
  715. //----------------------------------------------------------------------------------------
  716. //    FW_PrivFileSystem_IsValidFile
  717. //
  718. //  Return TRUE if the file exists.
  719. //----------------------------------------------------------------------------------------
  720.  
  721. FW_Boolean    SL_API         
  722. FW_PrivFileSystem_IsValidFile(Environment* ev, FW_OFileSpecification* theFile)
  723. {
  724.     FW_Boolean result = FALSE;
  725.  
  726.     FW_SOM_TRY
  727.     {
  728. #ifdef FW_BUILD_WIN
  729.         FW_CString fileName;
  730.         FW_TRY
  731.         {
  732.             theFile->GetFullPath(ev, fileName);
  733.     
  734.             char szFileName[_MAX_PATH];
  735.             fileName.ExportCString(szFileName);
  736.     
  737. #ifdef FW_BUILD_WIN16
  738.             FW_PlatformFileHandle fileHandle;
  739.             OFSTRUCT Buffer;
  740.     
  741.             fileHandle = ::OpenFile(szFileName, &Buffer, OF_EXIST);
  742.             if (fileHandle != FW_kInvalidAccessHandle)
  743.                 result = TRUE;
  744. #endif
  745.     
  746. #ifdef FW_BUILD_WIN32
  747.             long fileAttributes = 0;
  748.             
  749.             fileAttributes = ::GetFileAttributes(szFileName);
  750.             if (fileAttributes != (-1))
  751.                 if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  752.                     result = TRUE;
  753. #endif
  754.     
  755.         }
  756.         FW_CATCH_BEGIN
  757.         FW_CATCH_EVERYTHING()
  758.         {
  759.             result = FALSE;
  760.         }
  761.         FW_CATCH_END
  762. #endif
  763.  
  764. #ifdef FW_BUILD_MAC
  765.         FSSpec macSpec;
  766.         OSErr theError;
  767.         
  768.         theFile->MacGetFSSpec(ev, &macSpec);
  769.         theError = ::FSMakeFSSpec(macSpec.vRefNum, macSpec.parID, macSpec.name, &macSpec);
  770.         if (theError == noErr)
  771.             result = TRUE;
  772. #endif
  773.     }
  774.     FW_SOM_CATCH
  775.  
  776.     return (result);
  777. }
  778.  
  779.  
  780. #ifdef FW_BUILD_WIN
  781. //----------------------------------------------------------------------------------------
  782. //    FW_PrivFileSystem_WinPathExists
  783. //
  784. //  Return TRUE if the directory exists.
  785. //----------------------------------------------------------------------------------------
  786.  
  787. FW_Boolean    SL_API         
  788. FW_PrivFileSystem_WinPathExists(Environment* ev, FW_HString pathNameRep)
  789. {
  790.     FW_Boolean result = FALSE;
  791.  
  792.     FW_SOM_TRY
  793.     {
  794.         FW_CString pathName(pathNameRep);
  795.  
  796. #ifdef FW_BUILD_WIN16
  797.         FW_CString saveDirectory;
  798.         
  799.         FW_PrivFileSystemParser_PrivGetWorkingDirectory(saveDirectory);
  800.         const FW_Char* namePtr = (const FW_Char*)pathName;
  801.         short validDir = ::chdir((FW_Char*)namePtr);
  802.         if (validDir == 0)
  803.             result = TRUE;
  804.         
  805.         // Restore original directory.
  806.         namePtr = (const FW_Char*)saveDirectory;
  807.         ::chdir((FW_Char*)namePtr);
  808. #endif
  809.  
  810. #ifdef FW_BUILD_WIN32
  811.         long fileAttributes = 0;
  812.         
  813.         char szFileName[_MAX_PATH];
  814.         pathName.ExportCString(szFileName);
  815.     
  816.         fileAttributes = ::GetFileAttributes(szFileName);
  817.         if (fileAttributes != (-1))
  818.             if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  819.                 result = TRUE;
  820. #endif
  821.     }
  822.     FW_SOM_CATCH
  823.  
  824.     return (result);
  825. }
  826. #endif
  827.  
  828.  
  829. #ifdef FW_BUILD_MAC
  830. //----------------------------------------------------------------------------------------
  831. //    FW_PrivFileSystem_MacIsVolumeShared
  832. //
  833. //  Return TRUE if the volume specified by volumeRefNum is currently a shared volume.
  834. //----------------------------------------------------------------------------------------
  835.  
  836. FW_Boolean        SL_API     
  837. FW_PrivFileSystem_MacIsVolumeShared(Environment* ev, short volumeRefNum)
  838. {
  839.     // No try block necessary - Do not throw
  840. FW_UNUSED(ev);
  841.     FW_Boolean result = FALSE;
  842.     HParamBlockRec paramBlock;
  843.     GetVolParmsInfoBuffer volInfoBuffer;
  844.     FW_PlatformError theError = FW_xNoError;
  845.     
  846.     paramBlock.ioParam.ioNamePtr = NULL;
  847.     paramBlock.ioParam.ioVRefNum = volumeRefNum;
  848.     paramBlock.ioParam.ioBuffer = (Ptr)&volInfoBuffer;
  849.     paramBlock.ioParam.ioReqCount = sizeof(volInfoBuffer);
  850.     
  851.     theError = PBHGetVolParmsSync(¶mBlock);
  852.     if (theError == FW_xNoError) 
  853.     {
  854.         if (volInfoBuffer.vMAttrib & FW_kMacVolumeIsSharedBitMask)
  855.             result = TRUE;
  856.     }
  857.         
  858.     return (result);
  859. }
  860. #endif
  861.